package com.example.newsbackgroundmanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.newsbackgroundmanagement.entity.BaseModel;
import com.example.newsbackgroundmanagement.entity.Collectiblenews;
import com.example.newsbackgroundmanagement.entity.Collectiblepublicnews;
import com.example.newsbackgroundmanagement.entity.UserReleasenewsDto;
import com.example.newsbackgroundmanagement.mapper.CollectiblenewsMapper;
import com.example.newsbackgroundmanagement.service.ICollectiblenewsService;
import com.example.newsbackgroundmanagement.service.ICollectiblepublicnewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 邓旭波
 * @since 2023-06-26
 */
@Controller
@RequestMapping("/collectiblenews")
public class CollectiblenewsController {
    @Autowired
    ICollectiblenewsService iCollectiblenewsService;

    @Resource
    CollectiblenewsMapper collectiblenewsMapper;
    BaseModel baseModel;


    @ResponseBody
    @RequestMapping(value = "/getUserandReleasenews")//分别查询用户信息和用户发布的新闻信息
    public BaseModel getUserandReleasenews(@RequestBody Collectiblenews collectiblenews){
        System.out.println("-----------进入CollectiblenewsController的getUserandReleasenews!");
        UserReleasenewsDto userReleasenewsDto=collectiblenewsMapper.getUserandReleasenews(collectiblenews.getUid(),collectiblenews.getNid());
        if(userReleasenewsDto!=null){
            baseModel=new BaseModel(0,"查询数据成功!");
            baseModel.setData(userReleasenewsDto);
        }else{
            baseModel=new BaseModel(-1,"查询数据失败!");
        }
        return baseModel;
    }


    @ResponseBody
    @RequestMapping(value = "/InsertCollectibleNews")//插入一条用户收藏的新闻
    public BaseModel InsertCollectiblePublicNews(@RequestBody Collectiblenews collectiblenews){
        System.out.println("-----------进入CollectiblenewsController的InsertCollectibleNews!");
        System.out.println("--------------------"+collectiblenews);
        boolean flag=iCollectiblenewsService.save(collectiblenews);
        if(flag==true){
            System.out.println("------插入成功");
            baseModel = new BaseModel(121, "插入用户收藏的新闻成功!");
            baseModel.setData(collectiblenews);
        }else if(flag==false){
            System.out.println("------插入失败");
            baseModel = new BaseModel(122, "插入用户收藏的新闻失败!");
        }
        return baseModel;
    }



    @ResponseBody
    @RequestMapping(value = "/DeleteCollectibleNewsByUid")//根据uid删除用户收藏的新闻
    public BaseModel DeleteCollectibleNewsByUid(@RequestBody Collectiblenews collectiblenews){
        System.out.println("-----------进入CollectiblenewsController的DeleteCollectibleNewsByUid!");
        System.out.println("--------------------"+collectiblenews);
        QueryWrapper<Collectiblenews> collectiblenewsQueryWrapper=new QueryWrapper();
        collectiblenewsQueryWrapper.eq("uid",collectiblenews.getUid());
        boolean flag1=iCollectiblenewsService.remove(collectiblenewsQueryWrapper);
        if (flag1==true){
            System.out.println("------删除成功");
            baseModel = new BaseModel(131, "删除用户收藏的新闻成功!");
        }else if(flag1==false){
            System.out.println("------删除失败");
            baseModel = new BaseModel(132, "删除用户收藏的新闻失败!");
        }
        return  baseModel;
    }



    @ResponseBody
    @RequestMapping(value = "/DeleteCollectibleNewsByNid")//根据nid删除用户收藏的新闻
    public BaseModel DeleteCollectibleNewsByNid(@RequestBody Collectiblenews collectiblenews){
        System.out.println("-----------进入CollectiblenewsController的DeleteCollectibleNewsByNid");
        System.out.println("--------------------"+collectiblenews);
        QueryWrapper<Collectiblenews> collectiblenewsQueryWrapper=new QueryWrapper();
        collectiblenewsQueryWrapper.eq("nid",collectiblenews.getNid());
        boolean flag1=iCollectiblenewsService.remove(collectiblenewsQueryWrapper);
        if (flag1==true){
            System.out.println("------根据nid删除成功");
            baseModel = new BaseModel(191, "根据nid删除用户收藏的新闻成功!");
        }else if(flag1==false){
            System.out.println("------根据nid删除失败");
            baseModel = new BaseModel(192, "根据nid删除用户收藏的新闻失败!");
        }
        return  baseModel;
    }


    @ResponseBody
    @RequestMapping(value = "/SelectCollectibleNews")//查询所有用户收藏的新闻
    public BaseModel SelectCollectibleNews(){
        System.out.println("-----------进入CollectiblenewsController的SelectCollectibleNews!");
        QueryWrapper<Collectiblenews> collectiblenewsQueryWrapper=new QueryWrapper();
        List<Collectiblenews> collectiblenews=iCollectiblenewsService.list(collectiblenewsQueryWrapper);
        if(collectiblenews!=null){
            System.out.println("------查询成功");
            baseModel = new BaseModel(251, "查询所有用户收藏的新闻成功!");
            baseModel.setDatas(collectiblenews);
        }else{
            System.out.println("------查询失败");
            baseModel = new BaseModel(252, "查询所有用户收藏的新闻失败!");
        }
        return baseModel;
    }

    @ResponseBody
    @RequestMapping(value = "/SelectCollectibleNewsByUid")//根据uid查询用户收藏的新闻
    public BaseModel SelectCollectibleNewsByUid(@RequestBody Collectiblenews collectiblenews){
        System.out.println("-----------进入CollectiblenewsController的SelectCollectibleNewsByUid!");
        System.out.println("--------------------"+collectiblenews);
        QueryWrapper<Collectiblenews> collectiblenewsQueryWrapper=new QueryWrapper();
        collectiblenewsQueryWrapper.eq("uid",collectiblenews.getUid());
        List<Collectiblenews> collectiblenews1=iCollectiblenewsService.list(collectiblenewsQueryWrapper);
        if(collectiblenews1!=null){
            System.out.println("------查询成功");
            baseModel = new BaseModel(141, "查询用户收藏的新闻成功!");
            baseModel.setDatas(collectiblenews1);
        }else{
            System.out.println("------查询失败");
            baseModel = new BaseModel(142, "查询用户收藏的新闻失败!");
        }
        return baseModel;
    }



    @ResponseBody
    @RequestMapping(value = "/SelectCollectibleNewsByCollectState")//根据collectstate查询用户收藏的新闻
    public BaseModel SelectCollectibleNewsByCollectState(@RequestBody Collectiblenews collectiblenews){
        System.out.println("-----------进入CollectiblenewsController的SelectCollectibleNewsByCollectState!");
        System.out.println("--------------------"+collectiblenews);
        QueryWrapper<Collectiblenews> collectiblenewsQueryWrapper=new QueryWrapper();
        collectiblenewsQueryWrapper.eq("collectstate",collectiblenews.getCollectstate());
        List<Collectiblenews> collectiblenews1=iCollectiblenewsService.list(collectiblenewsQueryWrapper);
        if(collectiblenews1!=null){
            System.out.println("------根据状态查询成功");
            baseModel = new BaseModel(251, "根据状态查询用户收藏的新闻成功!");
            baseModel.setDatas(collectiblenews1);
        }else{
            System.out.println("------根据状态查询失败");
            baseModel = new BaseModel(252, "根据状态查询用户收藏的新闻失败!");
        }
        return baseModel;
    }


    @ResponseBody
    @RequestMapping(value = "/UpdateCollectibleNewsCollectStateByUid")//根据uid修改用户收藏的新闻的状态
    public BaseModel UpdateCollectibleNewsCollectStateByUid(@RequestBody Collectiblenews collectiblenews){
        System.out.println("-----------进入CollectiblenewsController的UpdateCollectibleNewsCollectStateByUid!");
        System.out.println("--------------------"+collectiblenews);
        UpdateWrapper<Collectiblenews> collectiblenewsUpdateWrapper=new  UpdateWrapper();
        collectiblenewsUpdateWrapper.set("collectstate",collectiblenews.getCollectstate()).eq("uid",collectiblenews.getUid());
        boolean g=iCollectiblenewsService.update(collectiblenewsUpdateWrapper);
        if(g==true){
            System.out.println("------根据用户ID修改状态成功");
            baseModel = new BaseModel(161, "根据用户ID修改用户收藏的新闻的状态成功!");
        }else{
            System.out.println("------根据用户ID修改状态失败");
            baseModel = new BaseModel(162, "根据用户ID修改用户收藏的新闻的状态失败!");
        }
        return baseModel;
    }


    @ResponseBody
    @RequestMapping(value = "/UpdateCollectibleNewsCollectStateByNid")//根据nid修改用户收藏的新闻的状态
    public BaseModel UpdateCollectibleNewsCollectStateByNid(@RequestBody Collectiblenews collectiblenews){
        System.out.println("-----------进入CollectiblenewsController的UpdateCollectibleNewsCollectStateByNid!");
        System.out.println("--------------------"+collectiblenews);
        UpdateWrapper<Collectiblenews> collectiblenewsUpdateWrapper=new  UpdateWrapper();
        collectiblenewsUpdateWrapper.set("collectstate",collectiblenews.getCollectstate()).eq("nid",collectiblenews.getNid());
        boolean h=iCollectiblenewsService.update(collectiblenewsUpdateWrapper);
        if(h==true){
            System.out.println("------根据nid修改状态成功");
            baseModel = new BaseModel(171, "根据nid修改用户收藏的新闻的状态成功!");
        }else{
            System.out.println("------根据nid修改状态失败");
            baseModel = new BaseModel(172, "根据nid修改用户收藏的新闻的状态失败!");
        }
        return baseModel;
    }




    @ResponseBody
    @RequestMapping(value = "/UpdateCollectibleNews")//修改用户收藏的新闻
    public BaseModel UpdateCollectibleNews(@RequestBody Collectiblenews collectiblenews){
        System.out.println("-----------进入CollectiblenewsController的UpdateCollectibleNews!");
        System.out.println("--------------------"+collectiblenews);
        QueryWrapper<Collectiblenews> collectiblenewsQueryWrapper=new QueryWrapper<>();
        collectiblenewsQueryWrapper.eq("nid",collectiblenews.getNid());
        if (iCollectiblenewsService.getOne(collectiblenewsQueryWrapper)!=null) {
            boolean c = iCollectiblenewsService.updateById(collectiblenews);
            if (c == true) {
                System.out.println("------修改成功");
                baseModel = new BaseModel(261, "修改用户收藏的新闻成功!");
            } else if (c == false) {
                System.out.println("------修改失败");
                baseModel = new BaseModel(262, "修改用户收藏的新闻失败!");
            }
        }else{
            baseModel = new BaseModel(263, "该用户收藏的新闻不存在!");
        }
        return baseModel;
    }
}
